import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);
const home = () => import("./../views/home/index");
const bestSeller = () => import("./../views/bestSeller/index.vue");
const sellerDetail = () => import("./../views/bestSeller/detail.vue");
const cart = () => import("./../views/cart/index.vue");
const login = () => import("./../views/user/login.vue");
const reset = () => import("./../views/user/reset.vue");
const register = () => import("./../views/user/register.vue");
const profile = () => import("./../views/user/profile.vue");
const userDetail = () => import("./../views/user/detail.vue");
const address = () => import("./../views/user/address.vue");
const order = () => import("./../views/user/order.vue");
const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: home,
    meta: {
      title: "Home",
    },
  },
  {
    path: "/bestSeller",
    name: "bestSeller",
    component: bestSeller,
    meta: {
      title: "Best Seller",
    },
  },
  {
    path: "/sellerDetail/:id",
    name: "sellerDetail",
    component: sellerDetail,
    meta: {
      title: "Seller Detail",
    },
  },
  {
    path: "/cart",
    name: "cart",
    component: cart,
    meta: {
      title: "Cart",
    },
  },
  {
    path: "/user/login",
    name: "login",
    component: login,
    meta: {
      title: "Login",
    },
  },
  {
    path: "/user/register",
    name: "register",
    component: register,
    meta: {
      title: "Register",
    },
  },
  {
    path: "/user/reset",
    name: "reset",
    component: reset,
    meta: {
      title: "reset",
    },
  },
  {
    path: "/user/profile",
    name: "profile",
    component: profile,
    meta: {
      title: "profile",
    },
  },
  {
    path: "/user/detail",
    name: "userDetail",
    component: userDetail,
    meta: {
      title: "Account Detail",
    },
  },
  {
    path: "/user/address",
    name: "userAddress",
    component: address,
    meta: {
      title: "Delivery Addresses",
    },
  },
  {
    path: "/user/order",
    name: "userOrder",
    component: order,
    meta: {
      title: "Order History",
    },
  },
];

const router = new Router({
  mode: "hash",
  routes,
  scrollBehavior() {
    return { x: 0, y: 0 };
  },
});

router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  next();
});

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

export default router;
